package com.rykj.api;

import android.util.Log;

import com.rykj.BuildConfig;
import com.rykj.api.converter.GsonConverterFactory;
import com.rykj.util.CacheHelper;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;


/**
 * Description: author: zhangsan on 16/11/21 下午1:30.
 */

@Deprecated
public class ApiManger {
    private static final long CONNECT_TIME_OUT = 20;
    private static final long READ_TIME_OUT = 20;
    private static OkHttpClient okHttpClient;
    private static Retrofit retrofit;
    private static ApiService apiService;

    public static String API_URL = "http://192.168.1.158:8888/";//龙
//    public static String API_URL = "http://192.168.1.171:8888/";//

    public static final String IMG_URL = "https://fishingcircles.oss-cn-beijing.aliyuncs.com/";//图片地址

    public ApiManger() {
        init();
    }
    @Deprecated
    public static void init() {
        //增加头部信息
        Interceptor headerInterceptor = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request build = chain.request().newBuilder()
                        .addHeader("Content-Type", "application/x-www-form-urlencoded")
                        .addHeader("accessToken", CacheHelper.getInstance().getToken()).build();
                Response response = chain.proceed(build);
                Log.i("info", response.request().url().toString());
                return response;
            }
        };
        if (okHttpClient == null) {
            OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
            builder.connectTimeout(CONNECT_TIME_OUT, TimeUnit.SECONDS)
//                    .addInterceptor(new AddCookiesInterceptor())
                    .addInterceptor(headerInterceptor).readTimeout(READ_TIME_OUT, TimeUnit.SECONDS)
                    .writeTimeout(READ_TIME_OUT, TimeUnit.SECONDS);
            if (BuildConfig.DEBUG) {
                builder.addInterceptor(new LoggingInterceptor());
            }
            okHttpClient = builder.build();

        }
        if (retrofit == null) {
            retrofit = new Retrofit.Builder().baseUrl(API_URL)
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create()).client(okHttpClient)
                    .build();
        }
    }
    @Deprecated
    public static ApiService getApiService() {
        if (null == apiService) {
            apiService = retrofit.create(ApiService.class);
        }
        return apiService;
    }
    @Deprecated
    public static ApiService createApiService() {
        return retrofit.create(ApiService.class);
    }
    @Deprecated
    public static OkHttpClient getOkHttpClient() {
        return okHttpClient;
    }

}
